import uvicorn
from fastapi import FastAPI,Request
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from typing import Optional

# 引入路由
from api.index_data import index_data as idxdta
from api.pie_data import pie_data as piedta
from api.sales_data import sales_data as sldta
from api.heatmap import heatmap as htdta

app = FastAPI()

app.include_router(idxdta, tags=['index'])
app.include_router(piedta, tags=['pie'])
app.include_router(sldta, tags=['sales'])
app.include_router(htdta, tags=['heatmap'])

# 配置静态文件目录
app.mount("/static", StaticFiles(directory="static"), name="static")

templates = Jinja2Templates(directory="templates")


@app.get("/", response_class=HTMLResponse)
def index(request: Request):
    return templates.TemplateResponse(
        request=request, name="index.html", context={}
    )

@app.get("/Frozen_pear.html", response_class=HTMLResponse)
def Frozen_pear(request: Request):
  return templates.TemplateResponse(
        request=request, name="Frozen_pear.html", context={}
    )


@app.get("/2020.html", response_class=HTMLResponse)
def first_year(request: Request):
    return templates.TemplateResponse(
        request=request, name="2020.html", context={}
    )

# Nutrient content per pear (example values in grams)
nutrient_content = {
    '水分': 84,
    '糖分': 10,
    '膳食纤维': 3,
    # Add other nutrients as needed
}

# Recommended daily intake for a healthy adult male (example values in grams)
daily_requirements = {
    '水分': 2500,  # Example: 2500 ml or grams
    '糖分': 50,    # Example: 50 grams
    '膳食纤维': 30, # Example: 30 grams
    # Add other nutrients as needed
}

class NutrientRequest(BaseModel):
    time: Optional[float] = 1  # Default to 1 day if not provided
    nutrient: str

@app.post("/calculate_pears")
def calculate_pears(request: NutrientRequest):
    nutrient = request.nutrient

    if nutrient in nutrient_content:
        daily_requirement = daily_requirements.get(nutrient, 0)
        content_per_pear = nutrient_content[nutrient]
        pears_needed = daily_requirement * request.time / content_per_pear
        return {"quantity": round(pears_needed, 2)}
    else:
        raise HTTPException(status_code=400, detail="Invalid nutrient")

if __name__ == '__main__':
    uvicorn.run(app="main:app")

